class DesignersController < ApplicationController
  layout 'base.html', :only => [:index, :show]

  def index
    respond_to do |format|
      format.html do
        @designers = Designer.all
        @html_title ="全体-设计师"
      end
      format.js do
        case
        when params[:department_id]
          @designers = Designer.find_all_by_department_id(params[:department_id])
          @html_title =  "#{Department.find(params[:department_id]).name}-设计师"
        when params[:designer_name]
          @designers = Designer.find_all_by_full_name(params[:designer_name])
          @html_title = "寻找设计师-#{params[:designer_name]}"
        else
          raise '未知操作'
        end
        render :layout => false
      end
      format.print do
        @designers = Designer.find_all_by_department_id(params[:department_id])
         @html_title = "打印【#{Department.find(params[:department_id]).name}】#{t params[:pay_status]}的项目提成"
         render :layout => 'base.print'
      end
    end
  end

  def show
    params[:stat] ||= {}
    params[:stat][:started_on] = params[:stat][:started_on].blank? ? (params[:payed_year].present? ? Date.new(params[:payed_year].to_i,1,1).to_s : Date.new(1949,10,1).to_s) : params[:stat][:started_on]
    params[:stat][:end_on] = params[:stat][:end_on].blank? ? (params[:payed_year].present? ? Date.new(params[:payed_year].to_i,1,1).at_end_of_year.to_s : Date.today.to_s) : params[:stat][:end_on]
    @designer = Designer.find(params[:id])
    if params[:pay_status] == 'payed'
      @projects = @designer.projects(params[:stat])
    else
      @projects = @designer.projects
    end
    respond_to do |format|
      format.html do
        @html_title = "【#{@designer.full_name}】的项目提成"
      end
      format.js do
        render :layout => false
      end
      format.print do
        @html_title = "打印【#{@designer.full_name}】#{t params[:pay_status]}项目的提成"
        render :layout => 'base.print'
      end
    end
  end

  def new
    @designer = Designer.new(
      :department_id => params[:department_id]
    )
  end

  def create
    @designer = Designer.new(params[:designer])
    render :new unless @designer.save
  end

  def edit
    @designer = Designer.find(params[:id])
  end

  def update
    @designer = Designer.find(params[:id])
    render :edit unless @designer.update_attributes(params[:designer])
  end

  def destroy
    @designer = Designer.find(params[:id])
    @designer.destroy if @designer.designer_work_items.empty?
  end

end

